package com.ityu.controller;

import com.ityu.exception.IllegalDataException;
import com.ityu.exception.InsertSameException;
import com.ityu.exception.UnableDeleteException;
import com.ityu.pojo.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class ExceptionController {

    @ExceptionHandler(InsertSameException.class)
    public Result handleInsertSameException(InsertSameException e) {
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(IllegalDataException.class)
    public Result handleIllegalDataException(IllegalDataException e) {
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(UnableDeleteException.class)
    public Result handleUnableDeleteException(UnableDeleteException e) {
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.error("服务器异常");
        e.printStackTrace();
        return Result.error("操作失败，未知错误！");
    }


}
